﻿Imports FinanceiroBLL
Public Class PessoaCON
    Dim objP As New ContasPagarBLL
    Public nome As String
    Public type As String
    Public codccc As integer
    Public codigo As Integer
    Public centrocusto As String
    Public codpes As Integer
    Public fornprest As String
#Region "Load"
    Private Sub PessoaCON_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CBFiltro.SelectedIndex = 1
        carregaGrid()

    End Sub
#End Region
#Region "Carrega Grid"
    Sub carregaGrid()
        Try
            Dim dt As New DataTable
            dt = objP.buscarP("", 5)
            DGVDados.DataSource = dt
            DGVDados.Refresh()

        Catch ex As Exception

        End Try
    End Sub

#End Region
#Region "Selected Index Changed"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
            '  CBStatus.Visible = True
            '  CBStatus.SelectedIndex = 0
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            '     CBStatus.Visible = False
        End If
        If TXTLocalizar.Text = "" Then
            TXTLocalizar.Focus()
            carregaGrid()
        End If
    End Sub
#End Region
#Region "CBStatus Selected Index Changed"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs)
        Dim sta As Integer = 0
        '     If (CBStatus.Text = "Ativo") Then sta = 1
        '  If (CBStatus.Text = "Excluído") Then sta = 2
        DGVDados.DataSource = objP.buscarP(sta, 2)
        DGVDados.Refresh()
    End Sub
#End Region
#Region "TXTLocalizar TextChanged"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = objP.buscarP(TXTLocalizar.Text, 0)
            Case "Razão Social"
                DGVDados.DataSource = objP.buscarP(TXTLocalizar.Text, 1)
            Case "Centro Custo"
                DGVDados.DataSource = objP.buscarP(TXTLocalizar.Text, 2)
                'Case "Status"
                '    DGVDados.DataSource = objP.buscarP(1, 5)
        End Select
        If TXTLocalizar.Text = "" Then
            'DGVDados.DataSource = objP.buscar("", 9)
            DGVDados.DataSource = objP.buscarP("", "")
        End If
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Data Grid Row Pre Paint"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "DGVDados Cell Double Click"
    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Try
            If type = "ret" Then
                Dim indice As Integer = -1
                'Percorre O DataGridView Verificando a Linha selecionada
                For Each linha As DataGridViewRow In DGVDados.SelectedRows
                    If (Not linha.Cells("for_codigo") Is Nothing) Then
                        'Guarda A Posição Selecionada
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    If (DGVDados.Rows(indice).Cells("sta_codigo").Value = 1) Then
                        fornprest = DGVDados.Rows(indice).Cells("for_razao").Value.ToString()
                        codpes = DGVDados.Rows(indice).Cells("for_codigo").Value.ToString()
                        centrocusto = DGVDados.Rows(indice).Cells("ccc_descricao").Value.ToString()
                        codccc = DGVDados.Rows(indice).Cells("ccc_codigo").Value.ToString()
                        Me.Close()
                    Else
                        Throw New Exception("Esta conta de Centro Custo está Inativo ou foi excluído!.Verifique status em Centro Custo")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region
#Region "Fechar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
        objP.limpar()
    End Sub
#End Region
#Region "Key Press"
    Private Sub PessoaCON_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
#End Region
End Class